﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using E.Web.UI.Page;
using E.Model;
using E.BLL;
using System.Data;
using X.Common;

namespace E.Web.Teacher.student
{
    public partial class Edit : TeacherBasePage
    {

        public string act;
        public int id;
        public IList<DictAreaModel> areaList;
        public Dictionary<int, string> certificateList;
        public UserModel model;
        public IDictionary<int, string> educationList;
        public IDictionary<int, string> isPublicList;
        private UserBLL bll = new UserBLL();
        private DataTable skillGroup;


        protected override void ShowPage()
        {
            act = XRequest.GetString("act");
            id = XRequest.GetInt("id", -1);
        }

        private bool CheckValue()
        {
            bool rst = true;

            if (XRequest.GetString("act") == "n" && LoginIDTxt.Text.Equals(""))
            {
                Alert("登录名不能为空");
                rst = false;
            }
            if (XRequest.GetString("act") == "n" && PwdTxt.Text.Equals(""))
            {
                Alert("密码不能为空");
                rst = false;
            }
            if (UserNameTxt.Text.Equals(""))
            {
                Alert("姓名不能为空");
                rst = false;
            }
            if (XRequest.GetString("act") == "n" && dlAreaSlt.SelectedValue.Equals("-1"))
            {
                Alert("请选择报名机构");
                rst = false;
            }
            if (XRequest.GetString("act") == "n" && dlCertificateSlt.SelectedValue.Equals("-1"))
            {
                Alert("请选择证书类别");
                rst = false;
            }

            if (dlIsPublicSlt.SelectedValue.Equals("-1") && XRequest.GetString("act") == "n")
            {
                Alert("请选择学员性质");
                rst = false;
            }

            return rst;
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (isPublicList == null)
                {
                    isPublicList = new Dictionary<int, string>();
                    isPublicList.Add(-1, "--请选择--");
                    isPublicList.Add(0, "内训");
                    isPublicList.Add(1, "外训");
                }

                if (areaList == null)
                {
                    DictAreaModel defaultModel = new DictAreaModel();
                    defaultModel.ID = -1;
                    defaultModel.Name = "--请选择--";
                    areaList = GetTeacherAreaList(teacherid);
                    areaList.Insert(0, defaultModel);
                }
                if (educationList == null)
                {
                    IDictionary<int, string> dbEducationList = bll.GetEducationList();
                    educationList = new Dictionary<int, string>();
                    educationList.Add(-1, "--请选择--");
                    foreach (int key in dbEducationList.Keys)
                    {
                        educationList.Add(key, dbEducationList[key]);
                    }
                }
                if (certificateList == null)
                {
                    Dictionary<int, string> dbTeacherProjectInfo;
                    if (authority == E.Enum.EnumTeacherAuthority.Internals)
                    {
                        dbTeacherProjectInfo = (new TeacherBLL()).GetTeacherProjectInfo(teacherid);
                    }
                    else
                    {
                        dbTeacherProjectInfo = (new CourseBLL()).GetAllCertificateAndProjectList();
                    }
                    certificateList = new Dictionary<int, string>();
                    certificateList.Add(-1, "--请选择--");
                    foreach (int key in dbTeacherProjectInfo.Keys)
                    {
                        certificateList.Add(key, dbTeacherProjectInfo[key]);
                    }
                }

                if (skillGroup == null)
                {
                    skillGroup = (new UserBLL()).GetSkillGroup();
                }
                initDropDownList();
                initUserData();
            }
        }

        private void initUserData()
        {
            if (act == "u" && id != -1)
            {
                model = (new UserBLL()).GetUserInfoByUserID(id);
                LoginIDTxt.Text = model.LoginID;
                UserNameTxt.Text = model.UserName;
                dlIsPublicSlt.SelectedValue = model.IsPublic == true ? "0" : "1";
                EmailTxt.Text = model.Email;
                IDCardTxt.Text = model.IDCard;
                dlEducationSlt.SelectedValue = model.EducationID.ToString();
                dlAreaSlt.SelectedValue = model.AreaID.ToString();
                dlCertificateSlt.SelectedValue = model.CurrentCertificateID.ToString();
                dlSkillGroup.SelectedValue = model.SkillGroupId.ToString();
                PositionTxt.Text = model.Position;
                LanTxt.Text = model.Lan;
                EntryDateTxt.Text = model.EntryDate;
                SeparationDateTxt.Text = model.SeparationDate;
                TrainingDateTxt.Text = model.TrainingDate;
                SkillCertificateTxt.Text = model.SkillCertificate;
                rdGender.SelectedValue = model.Gender == true ? "1" : "0";

            }
        }

        private void initDropDownList()
        {
            dlIsPublicSlt.DataSource = isPublicList;
            dlIsPublicSlt.DataTextField = "value";
            dlIsPublicSlt.DataValueField = "key";
            dlIsPublicSlt.DataBind();

            dlAreaSlt.DataSource = areaList;
            dlAreaSlt.DataTextField = "Name";
            dlAreaSlt.DataValueField = "ID";
            dlAreaSlt.DataBind();

            dlEducationSlt.DataSource = educationList;
            dlEducationSlt.DataTextField = "value";
            dlEducationSlt.DataValueField = "key";
            dlEducationSlt.DataBind();

            dlCertificateSlt.DataSource = certificateList;
            dlCertificateSlt.DataTextField = "value";
            dlCertificateSlt.DataValueField = "key";
            dlCertificateSlt.DataBind();

            dlSkillGroup.DataSource = skillGroup;
            dlSkillGroup.DataTextField = "SkillGroupName";
            dlSkillGroup.DataValueField = "Id";
            dlSkillGroup.DataBind();

        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            int rst = 0;
            string href = "";
            if (CheckValue())
            {

                model = new UserModel();

                model.UserName = UserNameTxt.Text.Trim();
                model.Gender = XRequest.GetInt("GenderRg", -1) == 1 ? true : false;
                model.Gender = rdGender.SelectedValue == "1" ? true : false;
                model.IsPublic = dlIsPublicSlt.SelectedValue == "1" ? true : false;
                model.Email = EmailTxt.Text.Trim();
                model.IDCard = IDCardTxt.Text.Trim();


                model.EducationID = int.Parse(dlEducationSlt.SelectedValue);
                model.ForgetPwdQuestionID = 1;
                model.ForgetPwdAnswer = "111111";

                model.SkillGroupId = int.Parse(dlSkillGroup.SelectedValue);
                model.Position = PositionTxt.Text.Trim();
                model.Lan = LanTxt.Text.Trim();
                model.SkillCertificate = SkillCertificateTxt.Text.Trim();

                model.EntryDate = EntryDateTxt.Text.Trim();
                model.SeparationDate = SeparationDateTxt.Text.Trim();
                model.TrainingDate = TrainingDateTxt.Text.Trim();

                model.LoginID = LoginIDTxt.Text.Trim();
                model.Pwd = Utils.MD5(PwdTxt.Text);
                model.RegisterTime = DateTime.Now;
                model.CurrentCertificateID = int.Parse(dlCertificateSlt.SelectedValue);
                model.AreaID = int.Parse(dlAreaSlt.SelectedValue);
                switch (act)
                {
                    case "n":
                        rst = bll.AddUser(model);
                        href = "/student/Edit.aspx?act=n";
                        break;
                    case "u":
                        model.ID = id;
                        rst = bll.UpdateUserInfo(model);
                        href = "/student/Edit.aspx?act=u&id=" + id;
                        break;
                    default:
                        break;

                }
            }


            if (rst > 0)
            {
                ExecuteJS("alert('操作成功'); window.location = '" + href + "';");
            }
            else
            {
                Alert("操作失败");
            }

        }

    }
}